<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_customer_invoice extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_customer_invoice_model', 'IspKontrakP');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	


	function index(){
		$data['title'] = "Client Invoice Berlangganan";	
		viewByTemplate('isp_customer_invoice/index', $data, 'grid');
	}

	function id_auto(){
		$query = $this->db->query("SELECT id_invoice FROM (SELECT * FROM isp_client_invoice ORDER BY id_invoice DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_invoice'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_invoice_auto'] = $autonum;

		$query = $this->db->query("SELECT no_invoice FROM (SELECT * FROM isp_client_invoice ORDER BY no_invoice DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['no_invoice'];
		else
			$MaxID = "000";

		$NextID = (int) substr($MaxID,0,3);

		$NextID++;

		$bulan = date("m");
		$tahun = date("Y");
			
			$hasil = "";
			$iromawi =
			array("01"=>"I","02"=>"II","03"=>"III",
					"04"=>"IV","05"=>"V","06"=>"VI",
					  "07"=>"VII","08"=>"VIII","09"=>"IX",
					  	"10"=>"X","11"=>"XI","12"=>"XII"
				/*20=>"XX",30=>"XXX",40=>"XL",50=>"L",60=>”LX”,70=>”LXX”,80=>”LXXX”,
				90=>”XC”,100=>”C”,200=>”CC”,300=>”CCC”,400=>”CD”,500=>”D”,
				600=>”DC”,700=>”DCC”,800=>”DCCC”,900=>”CM”,1000=>”M”,
				2000=>”MM”,3000=>”MMM”*/);
			if(array_key_exists($bulan,$iromawi)){
				$hasil = $iromawi[$bulan];
			}/*elseif($bulan >= 11 && $bulan <= 99){
				$i = $bulan % 10;
				$hasil = $iromawi[$bulan-$i] . Romawi($bulan % 10);
			}elseif($bulan >= 101 && $bulan <= 999){
				$i = $bulan % 100;
				$hasil = $iromawi[$bulan-$i] . Romawi($bulan % 100);
			}else{
				$i = $bulan % 1000;
				$hasil = $iromawi[$bulan-$i] . Romawi($bulan % 1000);
			}
			return $hasil;*/

			//var_dump($bulan);

		$data['no_invoice_auto'] = sprintf("%03s",$NextID) . "/BL/CYBER-K/" . $hasil . "/" . $tahun;
		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON2();
	}	

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		//Validation		
		if (post("no_invoice") == '0') {
			$error->Add("no_invoice", "No Invoice", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_client_invoice WHERE no_invoice = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no_invoice"))))."'")) > 0) {
			$error->Add("no_invoice", "No Invoice", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'kode_kontrak' => $this->input->post('kode_kontrak'),
					'kode_customer' => $this->input->post('kode_customer'),
					'nama_customer' => $this->input->post('nama_customer'),
					'abodemen' => $this->input->post('abodemen'),
					'total' => $this->input->post('total'),
					'ppn' => $this->input->post('ppn'),
					'no_invoice' => $this->input->post('no_invoice'),
					'nama_jenis_barang' => $this->input->post('nama_jenis_barang'),
					'bandwidth' => $this->input->post('bandwidth'),
					'tanggal_invoice' => $this->input->post('tanggal_invoice'),
					'status' => $this->input->post('status'),
					'tanggal_bayar' => $this->input->post('tanggal_bayar'),
					'due_date' => $this->input->post('due_date'),
					'periode_tagihan' => $this->input->post('periode_bulan').'-'.$this->input->post('periode_tahun'),					
					'keterangan' => $this->input->post('keterangan'),
					'biaya_pendaftaran' => $this->input->post('biaya_pendaftaran'),
					'terbilang' => $this->input->post('terbilang'),
					'id_invoice' => $this->input->post('id_invoice'),
					'id_kontrak' => $this->input->post('id_kontrak'),
					'input_ppn' => $this->input->post('input_ppn')
					);

					$data['tanggal_invoice']	= ($data['tanggal_invoice'] == '' ? NULL : date_format3($data['tanggal_invoice'], 'YYYY-MM-DD'));
					$data['tanggal_bayar']	= ($data['tanggal_bayar'] == '' ? NULL : date_format3($data['tanggal_bayar'], 'YYYY-MM-DD'));
					$data['due_date']	= ($data['due_date'] == '' ? NULL : date_format3($data['due_date'], 'YYYY-MM-DD'));


				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspKontrakP->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspKontrakP->Update($data, 
										array('id_invoice'	=> $data['id_invoice']));

					$ls_response = "Data Tersimpan [Edited]";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "Data Terhapus";
		$id_invoice 	= get('id_invoice');
		
		if ($id_invoice != "")
		{
			// Delete Single
			$arr_rule	= array('id_invoice' => $id_invoice);
			$ls_result = $this->IspKontrakP->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_invoice' => $row['id_invoice']);
				$ls_result .= $this->IspKontrakP->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}

}
?>